<?php

require_once dirname(__FILE__).'/../lib/disbursementGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/disbursementGeneratorHelper.class.php';

/**
 * disbursement actions.
 *
 * @package    sf_sandbox
 * @subpackage disbursement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class disbursementActions extends autoDisbursementActions
{
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $requestparams=$request->getParameter($form->getName());
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    $error="";
    
    if($requestparams["date"]["day"]=="" or $requestparams["date"]["month"]=="" or $requestparams["date"]["year"]=="")
      $error="Invalid date: ".$requestparams["date"]["month"]."/".$requestparams["date"]["day"]."/".$requestparams["date"]["year"];

    if($requestparams["disbursement_amount"]<=0)
      $error="Invalid disbursement amount: ".$requestparams["disbursement_amount"];

    if($requestparams["release_amount"]<=0)
      $error="Invalid release amount: ".$requestparams["release_amount"];
    
    $contract=ContractTable::fetchById($requestparams["contract_id"]);
    var_dump($contract->getContractor());
    if($requestparams["release_amount"]+$contract["total_release"]>$contract["bid_amount"])
      $error="Release amount causes total release to exceed contract amount: ".$requestparams["release_amount"];

    if($error)
    {
      $this->getUser()->setFlash('error', $error);
      $this->redirect($this->request->getReferer());
    }
    elseif ($form->isValid())
    {
      $this->getUser()->setFlash('notice', $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.');

      $disbursement = $form->save();
      $contract=$disbursement->getContract();
      $contract->calc();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $disbursement)));

      $this->redirect('@project_show?id='.$disbursement->getContract()->getProjectId());
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
      //$this->redirect($this->request->getReferer());
    }
  }
  public function executeDelete(sfWebRequest $request)
  {
    //$request->checkCSRFProtection();
    if($request->getParameter("disbursement[_csrf_token]")=="")die();

    //$this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

    $this->forward404Unless($disbursement = Doctrine::getTable('Disbursement')->find(array($request->getParameter('disbursement[id]'))), sprintf('Object disbursement does not exist (%s).', array($request->getParameter('id'))));


//    $disbursement=$this->getRoute()->getObject();
    $contract=$disbursement->getContract();

    $disbursement->delete();
    $contract->calc();

    $this->getUser()->setFlash('notice', 'The item was deleted successfully.');

    $this->redirect($this->request->getReferer());
  }
}
